import { defineStore } from 'pinia';
import { USER_INFO_KEY } from '@/enums/cacheEnum';
import { setCache, getCache } from '@/utils/catch';
interface UserState {
  user?: UserModel;
}

export const useUserStore = defineStore({
  id: 'user',
  state: (): UserState => ({
    user: undefined,
  }),
  getters: {
    isManage: (state) => {
      return state.user?.is_manage;
    },
    getUser: (state) => {
      return state.user || getCache(USER_INFO_KEY) || undefined;
    },
  },
  actions: {
    setUser(user: UserModel | undefined) {
      setCache(USER_INFO_KEY, user);
      this.user = user;
    },
  },
});
